package com.netease.homework.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.netease.homework.dao.ICommodityDao;
import com.netease.homework.dao.ISelledDao;
import com.netease.homework.dao.IUserDao;
import com.netease.homework.meta.Commodity;
import com.netease.homework.meta.Selled;
import com.netease.homework.meta.User;
import com.netease.homework.service.ISelledService;

@Component
@Qualifier("SelledService")
public class SelledServiceImpl implements ISelledService {

	@Autowired
	private ISelledDao selledDao;
	
	@Autowired
	private IUserDao userDao;
	
	@Autowired
	private ICommodityDao commodityDao;

	@Override
	public Map<String, Object> getSelledById(Integer id) {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		Selled selled = selledDao.getSelledById(id);

		if (selled != null) {
			returnMap.put("value", selled);
			returnMap.put("message", "获取成功");
			returnMap.put("success", true);
		} else {
			returnMap.put("message", "失败");
			returnMap.put("success", false);
		}

		return returnMap;
	}

	@Override
	public Map<String, Object> addSelled(Integer userId, Integer commodityId, Date time, Integer number, Double price) {

		Map<String, Object> returnMap = new HashMap<String, Object>();

		Selled selled = new Selled();

		selled.setUserId(userId);
		selled.setTime(time);
		selled.setNumber(number);
		selled.setPrice(price);
		
		Commodity commodity = commodityDao.getCommById(commodityId);
		if (commodity!=null) {
			selled.setCommodity(commodity);
			int result = selledDao.addSelled(selled);
			if (result > 0) {
				returnMap.put("value", result);
				returnMap.put("message", "成功");
				returnMap.put("success", true);
			}
		} else {
			returnMap.put("message", "失败");
			returnMap.put("success", false);
		}

		return returnMap;
	}

	@Override
	public Map<String, Object> getSelledByUserName(String userName) {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		List selled = selledDao.getSelledByUserName(userName);

		if (selled != null) {
			returnMap.put("value", selled);
			returnMap.put("message", "获取成功");
			returnMap.put("success", true);
		} else {
			returnMap.put("message", "失败");
			returnMap.put("success", false);
		}

		return returnMap;
	}

	@Override
	public Map<String, Object> addSelledByName(String userName, Integer commodityId, Integer number,
			Double price) {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		Selled selled = new Selled();
		
		Date time = new Date();
		selled.setTime(time);
		selled.setNumber(number);
		selled.setPrice(price);
		
		User user = userDao.getUserByName(userName);
		Commodity commodity = commodityDao.getCommById(commodityId);
		commodity.setIsSelled(true);
		commodityDao.editComm(commodity);
		if (commodity!=null && user!=null) {

			selled.setUserId(user.getId());
			selled.setCommodity(commodity);
			int result = selledDao.addSelled(selled);
			if (result > 0) {
				returnMap.put("value", result);
				returnMap.put("message", "成功");
				returnMap.put("success", true);
			}
		} else {
			returnMap.put("message", "失败");
			returnMap.put("success", false);
		}

		return returnMap;
	}

	@Override
	public Map<String, Object> addSelledListByName(List<Selled> list) {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		for (Selled selledInList : list) {
			Selled selled = new Selled();
			Date time = new Date();
			selled.setTime(time);
			selled.setNumber(selledInList.getNumber());
			selled.setPrice(selledInList.getPrice());
			Commodity commodity = commodityDao.getCommById(selledInList.getCommodity().getId());
			commodity.setIsSelled(true);
			commodityDao.editComm(commodity);
			if (commodity!=null ) {

				selled.setUserId(selledInList.getUserId());
				selled.setCommodity(commodity);
				int result = selledDao.addSelled(selled);
				if (result > 0) {
					returnMap.put("value", result);
					returnMap.put("message", "成功");
					returnMap.put("success", true);
				}
			} else {
				returnMap.put("message", "失败");
				returnMap.put("success", false);
				break;
			}
		}
		
		
		
		

		return returnMap;
	}

	@Override
	public Map<String, Object> getSelledByCommId(Integer id) {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		Selled selled = selledDao.getSelledByCommId(id);

		if (selled != null) {
			returnMap.put("value", selled);
			returnMap.put("message", "获取成功");
			returnMap.put("success", true);
		} else {
			returnMap.put("message", "失败");
			returnMap.put("success", false);
		}

		return returnMap;
	}

}
